package dao;

import org.apache.ibatis.annotations.*;
import pojo.PatientDoctor;

import java.util.List;

@Mapper
public interface PatientDoctorMapper {

    @Select("SELECT * FROM patient_doctor")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "patient_id", property = "patientId"),
            @Result(column = "name", property = "name"),
            @Result(column = "doc_id", property = "docId"),
            @Result(column = "telephone", property = "telephone"),
            @Result(column = "day", property = "day"),
            @Result(column = "depart_id2", property = "departId2"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "info", property = "info"),
            @Result(column = "diagnosis_status", property = "diagnosisStatus") // 新增映射
    })
    List<PatientDoctor> findAll();

    @Select("SELECT * FROM patient_doctor WHERE id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "patient_id", property = "patientId"),
            @Result(column = "name", property = "name"),
            @Result(column = "doc_id", property = "docId"),
            @Result(column = "telephone", property = "telephone"),
            @Result(column = "day", property = "day"),
            @Result(column = "depart_id2", property = "departId2"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "info", property = "info"),
            @Result(column = "diagnosis_status", property = "diagnosisStatus") // 新增映射
    })
    PatientDoctor findById(int id);

    @Insert("INSERT INTO patient_doctor(" +
            "patient_id, name, doc_id, telephone, day, depart_id2, info, diagnosis_status) " +
            "VALUES(#{patientId}, #{name}, #{docId}, #{telephone}, #{day}, #{departId2}, #{info}, #{diagnosisStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addPatientDoctor(PatientDoctor patientDoctor);

    @Update("UPDATE patient_doctor SET " +
            "patient_id = #{patientId}, " +
            "name = #{name}, " +
            "doc_id = #{docId}, " +
            "telephone = #{telephone}, " +
            "day = #{day}, " +
            "depart_id2 = #{departId2}, " +
            "info = #{info}, " +
            "diagnosis_status = #{diagnosisStatus} " + // 新增字段更新
            "WHERE id = #{id}")
    int updatePatientDoctor(PatientDoctor patientDoctor);

    @Delete("DELETE FROM patient_doctor WHERE id = #{id}")
    int deletePatientDoctor(int id);

    @Select("SELECT * FROM patient_doctor WHERE doc_id = #{docId}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "patient_id", property = "patientId"),
            @Result(column = "name", property = "name"),
            @Result(column = "doc_id", property = "docId"),
            @Result(column = "telephone", property = "telephone"),
            @Result(column = "day", property = "day"),
            @Result(column = "depart_id2", property = "departId2"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "info", property = "info"),
            @Result(column = "diagnosis_status", property = "diagnosisStatus") // 新增映射
    })
    List<PatientDoctor> findByDocId(String docId);

    // 新增：直接将诊断状态设为1（已诊断）
    @Update("UPDATE patient_doctor SET diagnosis_status = 1 WHERE id = #{id}")
    int markAsDiagnosed(int id);

    @Select("SELECT COUNT(*) FROM patient_doctor WHERE patient_id = #{patientId} AND doc_id = #{docId} AND day = #{day}")
    int countByPatientDocDay(@Param("patientId") String patientId, @Param("docId") String docId, @Param("day") String day);
}